package org.dxy.trigger.http;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dxy.api.dto.request.mbom.*;
import org.dxy.api.response.R;
import org.dxy.infrastructure.persistent.db.mybatispuls.po.query.MBOMViewPo;
import org.dxy.trigger.http.service.MBOMApplicationService;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * MBOM管理控制器
 * @author dxy
 * @createTime 2025/9/10
 */
@Slf4j
@RestController
@RequestMapping("/api/mbom")
@RequiredArgsConstructor
public class MBOMController {

    private final MBOMApplicationService mbomApplicationService;

    /**
     * 从EBOM同步创建MBOM
     */
    @PostMapping("/sync-from-ebom")
    public R<Long> syncFromEBOM(@Validated @RequestBody SyncMBOMFromEBOMRequest request) {
        return mbomApplicationService.syncFromEBOM(request);
    }

    /**
     * 创建MBOM
     */
    @PostMapping("/create")
    public R<Long> create(@Validated @RequestBody CreateMBOMRequest request) {
        return mbomApplicationService.create(request);
    }

    /**
     * 分页查询MBOM列表
     */
    @GetMapping("/list")
    public R<Page<MBOMViewPo>> list(
            @ParameterObject Page<MBOMViewPo> page,
            @ParameterObject MBOMViewPo query) {
        return mbomApplicationService.list(page, query);
    }

    /**
     * 更新MBOM信息
     */
    @PutMapping("/{id}")
    public R<Void> update(@PathVariable Long id, @Validated @RequestBody UpdateMBOMRequest request) {
        return mbomApplicationService.update(id, request);
    }

    /**
     * 提交审核
     */
    @PutMapping("/{id}/submit-review")
    public R<Void> submitForReview(@PathVariable Long id, @Validated @RequestBody SubmitMBOMReviewRequest request) {
        return mbomApplicationService.submitForReview(id, request);
    }

    /**
     * 审核通过
     */
    @PutMapping("/{id}/approve")
    public R<Void> approve(@PathVariable Long id, @Validated @RequestBody ApproveMBOMRequest request) {
        return mbomApplicationService.approve(id, request);
    }

    /**
     * 审核拒绝
     */
    @PutMapping("/{id}/reject")
    public R<Void> reject(@PathVariable Long id, @Validated @RequestBody RejectMBOMRequest request) {
        return mbomApplicationService.reject(id, request);
    }

    /**
     * 发布MBOM
     */
    @PutMapping("/{id}/release")
    public R<Void> release(@PathVariable Long id) {
        return mbomApplicationService.release(id);
    }

    /**
     * 分配给工单
     */
    @PutMapping("/{id}/assign-workorder")
    public R<Void> assignToWorkOrder(@PathVariable Long id, @Validated @RequestBody AssignMBOMToWorkOrderRequest request) {
        return mbomApplicationService.assignToWorkOrder(id, request);
    }

    /**
     * 作废MBOM
     */
    @PutMapping("/{id}/obsolete")
    public R<Void> obsolete(@PathVariable Long id, @Validated @RequestBody ObsoleteMBOMRequest request) {
        return mbomApplicationService.obsolete(id, request);
    }

    /**
     * 获取MBOM详情
     */
    @GetMapping("/{id}")
    public R<MBOMViewPo> getById(@PathVariable Long id) {
        return mbomApplicationService.getById(id);
    }

    /**
     * 根据产品获取有效MBOM列表
     */
    @GetMapping("/product/{productId}/active")
    public R<List<MBOMViewPo>> getActiveByProductId(@PathVariable Long productId) {
        return mbomApplicationService.getActiveByProductId(productId);
    }

    /**
     * 根据状态获取MBOM列表
     */
    @GetMapping("/status/{status}")
    public R<List<MBOMViewPo>> getByStatus(@PathVariable String status) {
        return mbomApplicationService.getByStatus(status);
    }
}